package charles.testproject.guidemos;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

public class GridLayoutDemo extends JFrame{

	private final String names[] = {"one","two","three","four","five","six"};
	private Button buttons[] = new Button[6];
	boolean flag = false;
	private GridLayout gy1 = new GridLayout(2,3,5,10);
	private GridLayout gy2 = new GridLayout(3,2);
	
	public GridLayoutDemo(String title) {
		super.setTitle(title);
		setLayout();
		ActionListener setLayoutListener = new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				setLayout();
				GridLayoutDemo.this.validate();
			}
		};
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		for(int i = 0; i < 6; i++) {
			buttons[i] = new Button(names[i]);
			buttons[i].addActionListener(setLayoutListener);
			this.add(buttons[i]);
		}
		this.setSize(300,200);
		this.setVisible(true);
	}
	private void setLayout() {
		if(flag) {
			this.setLayout(gy1);
		}
		else {
			this.setLayout(gy2);
		}
		flag = !flag;
	}
	public static void main(String[] args) {
		GridLayoutDemo gld = new GridLayoutDemo("hee,man");
	}

}
